package mao.thinkinjava.io;

import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.nio.channels.FileChannel;

/**
 * java 新IO
 */
public class TransferTo {
    public static void main(String[] args) throws IOException {
        FileChannel in = new FileInputStream("data.txt").getChannel();
        //需要设置追加为true否则，程序会删除文件，再重新建立一遍文件
        FileChannel out = new FileOutputStream("data_copy.txt",true).getChannel();
        out.position(out.size());
        in.transferTo(0,in.size(),out);
        //若是传入参数的position大于文件size，则没有字节被转移。
        long l = out.transferFrom(in, out.size(), 10);
        System.out.println(l);

        in.close();
        out.close();
    }
}
